package com.hst.web.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 这个类是辅助计算datetime的类,包含日期和时间相关处理 
 * 
 * @author duyong
 */
public class DateTimeUtil {
	
    /**
     * 返回两个日期之间隔了多少天
     * @param beginDate	开始日期 ,不能为空(null)
     * @param EndDate	结束日期 ,不能为空(null)
     * @return
     */
    public static int DateDiff(Date beginDate, Date EndDate) {
    	int i = (int) ((EndDate.getTime() - beginDate.getTime()) / 3600 / 24 / 1000);
    	return i;
    }
    
	/**
	 * 日期加减
	 * @return 加减后的日期
	 * @param beginDate 开始日期
	 * @param field 1-年份,2-月份,3-周,5-天,10-时,12-分,13-秒
	 * @param amount 加减数
	 */
    public static Date calendarAdd(Date beginDate, int field, int amount){ 
		GregorianCalendar gc = new GregorianCalendar(); 
		gc.setTime((java.util.Date) beginDate);
		gc.add(field, amount);
		java.util.Date newDate = gc.getTime();
		return newDate;
    }
    
    /**   
     * @param beginDate 需要比较的时间 不能为空(null),需要正确的日期格式   
     * @param EndDate   被比较的时间 不能为空(null),需要正确的日期格式   
     * @param field 1-年份,2-月份,3-周,5-天
     * @return   
     */ 
    public static int calendarDiff(Date beginDate, Date EndDate, int field){   
        int n = 0;
        GregorianCalendar gc1 = new GregorianCalendar();
        GregorianCalendar gc2 = new GregorianCalendar();
        try {  
        	gc1.setTime((java.util.Date) beginDate);
        	gc2.setTime((java.util.Date) EndDate);
        } catch (Exception e3) {   
            System.out.println("wrong occured");
        }   
        if (!gc1.after(gc2)) {
            while (!gc1.after(gc2)) {                   // 循环对比，直到相等，n 就是所要的结果   
                n++;
                gc1.add(field, 1);          // 比较月份，月份+1
            }
            n = n-1;
        } else {
            while (gc1.after(gc2)) {                   	// 循环对比，直到相等，n 就是所要的结果
                n--;
                gc1.add(field, -1);          // 比较月份，月份-1
            }
        }
        return n;
    }
    
    /** 
     * 获取上一年年份
     * @param
     * @return String
     */ 
    public static String getLastYear(){
        Calendar calendar = Calendar.getInstance();
        return Integer.toString(calendar.get(Calendar.YEAR) - 1);
    }
    
    /** 
     * 获取当前年份
     * @param
     * @return String
     */ 
    public static String getCurrYear(){
        Calendar calendar = Calendar.getInstance();
        return Integer.toString(calendar.get(Calendar.YEAR));
    }
    
    /** 
     * 获取下一年年份
     * @param
     * @return String
     */ 
    public static String getNextYear(){
        Calendar calendar = Calendar.getInstance();
        return Integer.toString(calendar.get(Calendar.YEAR) + 1);
    }
    
    /** 
     * 获取某天年份
     * @param  Date
     * @return String 
     */ 
    public static String getYear(Date date){
        Calendar calendar = Calendar.getInstance(); 
        calendar.setTime(date);
        return Integer.toString(calendar.get(Calendar.YEAR));
    }
    
    /** 
     * 获取当前月份
     * @param
     * @return 1-一月,2-二月,3-三月,...,10-十月,11-十一月,12-十二月
     */
    public static String getCurrMonth(){
        Calendar calendar = Calendar.getInstance();
        return Integer.toString(calendar.get(Calendar.MONTH) + 1);
    }
    
    /** 
     * 获取某天月份
     * @param  Date
     * @return String 
     */ 
    public static String getMonth(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return Integer.toString(calendar.get(Calendar.MONTH) + 1);
    }
    
    /** 
     * 获取当前当天是星期几
     * @return 1-星期日,2-星期一,3-星期二,4-星期三,5-星期四,6-星期五,7-星期六
     */ 
    public static String getDayForWeek(){
        Calendar calendar = Calendar.getInstance();
        return Integer.toString(calendar.get(Calendar.DAY_OF_WEEK));
    }
    
    /** 
     * 获取当前日期
     * @param 
     * @return String
     */ 
    public static String getCurrDate(String pattern){
    	Date currDate = new Date();
    	SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    	return sdf.format(currDate);
    }
 
    /** 
     * 获取当年的第一天 
     * @return Date
     */ 
    public static Date getCurrYearFirst(){ 
        Calendar calendar = Calendar.getInstance();
        int currYear = calendar.get(Calendar.YEAR); 
        return getYearFirst(currYear);
    }
    
    /** 
     * 获取当年的最后一天 
     * @return Date
     */ 
    public static Date getCurrYearLast(){ 
        Calendar calendar=Calendar.getInstance();
        int currYear = calendar.get(Calendar.YEAR);
        return getYearLast(currYear);
    }
    
    /** 
     * 获取下一年的最后一天 
     * @return Date
     */ 
    public static Date getNextYearLast(){ 
        Calendar calendar=Calendar.getInstance();
        int currYear = calendar.get(Calendar.YEAR);
        return getYearLast(currYear + 1);
    }
    
    /** 
     * 获取某年第一天日期 
     * @param year 年份 
     * @return Date 
     */ 
    public static Date getYearFirst(int year){ 
        Calendar calendar = Calendar.getInstance(); 
        calendar.clear(); 
        calendar.set(Calendar.YEAR, year);
        return calendar.getTime();
    }
    
    /** 
     * 获取某年第一天日期 
     * @param  Date
     * @return Date 
     */ 
    public static Date getYearFirst(Date date){ 
        Calendar calendar = Calendar.getInstance(); 
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    /** 
     * 获取某年最后一天日期 
     * @param year 年份 
     * @return Date 
     */ 
    public static Date getYearLast(int year){ 
        Calendar calendar = Calendar.getInstance(); 
        calendar.clear(); 
        calendar.set(Calendar.YEAR, year); 
        calendar.roll(Calendar.DAY_OF_YEAR, -1); 
        return calendar.getTime(); 
    }

    /** 
     * 获取某年最后一天日期 
     * @param  Date
     * @return Date 
     */ 
    public static Date getYearLast(Date date){ 
        Calendar calendar = Calendar.getInstance(); 
        calendar.setTime(date);
        calendar.set(Calendar.MONTH, 11);
        calendar.set(Calendar.DAY_OF_MONTH, 31);
        return calendar.getTime();
    }
    /** 
     * 获取当月的第一天 
     * @param 
     * @return Date
     */ 
    public static Date getCurrMonthFirst(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }
    
    /** 
     * 获取下月的第一天 
     * @param 
     * @return Date
     */ 
    public static Date getNextMonthFirst(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
	}
    
    /** 
     * 获取当月的最后一天 
     * @param
     * @return Date
     */
    public static Date getCurrMonthLast(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));    	
        return calendar.getTime();
    }
    
    /** 
     * 获取下月的最后一天 
     * @param
     * @return Date
     */
    public static Date getNextMonthLast(){
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));  
        return calendar.getTime();
    }
    
    /** 
     * 获取某月第一天日期 
     * @param year 年份, month 月份
     * @return Date 
     */ 
    public static Date getMonthFirst(int year, int month){
        Calendar calendar = Calendar.getInstance(); 
        calendar.clear(); 
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1);
        return calendar.getTime();
    }

    /** 
     * 获取某月第一天日期 
     * @param Date
     * @return Date 
     */ 
    public static Date getMonthFirst(Date date){
        Calendar calendar = Calendar.getInstance(); 
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }
    
    /** 
     * 获取某月最后一天日期
     * @param year 年份, month 月份
     * @return Date 
     */ 
    public static Date getMonthLast(int year, int month){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return calendar.getTime();
    }
    
    /** 
     * 获取某月最后一天日期
     * @param Date
     * @return Date 
     */ 
    public static Date getMonthLast(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); 
        return calendar.getTime();
    }
    
    /**
     * 
     * 字符串转换为对应日期
     * 
     * @param source
     * @param pattern
     * @return
     */
    public static Date stringToDate(String source, String pattern) {
    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = simpleDateFormat.parse(source);
        } catch (Exception e) {
        }
        return date;
    }
    
    /**
     * 
     * 日期转换为对应字符串
     * 
     * @param date
     * @param pattern
     * @return
     */
    public static String dateToString(Date date, String pattern) {
    	SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        String strDate = null;
        try {
        	strDate = simpleDateFormat.format(date);
        } catch (Exception e) {
        }
        return strDate;
    }
}

